﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using AdvUtils;

namespace BuildLocationDictMatchData
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length <= 2)
            {
                Console.WriteLine("BuildLocationDictMatchData [dict match binary dict file name] [black term list file name] [raw text dict file name] ...");
                return;
            }

            //Load black list
            HashSet<string> setBlackList = new HashSet<string>();
            if (File.Exists(args[1]) == true)
            {
                StreamReader sr_black_list = new StreamReader(args[1]);
                while (sr_black_list.EndOfStream == false)
                {
                    string strLine = sr_black_list.ReadLine();
                    setBlackList.Add(strLine);
                }
                sr_black_list.Close();

            }
            else
            {
                Console.WriteLine("File {0} is not existed.", args[1]);
            }

            StreamWriter sw = new StreamWriter("BuildLocationDictMatchData.tmp");
            Dictionary<string, List<string>> key2valList = new Dictionary<string, List<string>>();

            for (int i = 2; i < args.Length; i++)
            {
                StreamReader sr = new StreamReader(args[i]);
                Console.WriteLine("Reading raw text file {0}...", args[i]);

                while (sr.EndOfStream == false)
                {
                    string strLine = sr.ReadLine();
                    string[] items = strLine.Split('\t');
                    string[] aTags = items[1].Split('|');

                    if (setBlackList.Contains(items[0]) == true)
                    {
                        continue;
                    }

                    if (key2valList.ContainsKey(items[0]) == false)
                    {
                        key2valList.Add(items[0], new List<string>());
                    }

                    foreach (string strTag in aTags)
                    {
                        if (key2valList[items[0]].Contains(strTag) == false)
                        {
                            key2valList[items[0]].Add(strTag);
                        }
                    }

                }
                sr.Close();
            }

            foreach (KeyValuePair<string, List<string>> pair in key2valList)
            {
                string strTagList = "";
                foreach (string item in pair.Value)
                {
                    strTagList = strTagList + "|" + item;
                }
                sw.WriteLine("{0}\t{1}", pair.Key, strTagList.Substring(1));
            }
            sw.Close();

            DictMatch dm = new DictMatch();
            dm.ConvertDictFromRawTextToBinary("BuildLocationDictMatchData.tmp", args[0]);

            Console.WriteLine("Build binary dict match dictionary file successed.");
        }
    }
}
